package com.tianxiao.faas.application.handler;

import com.tianxiao.faas.common.exception.biz.AuthenticationException;
import com.tianxiao.faas.common.exception.biz.BizException;
import com.tianxiao.faas.common.exception.biz.PermissionException;
import com.tianxiao.faas.common.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class AuthenticationExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger(AuthenticationExceptionHandler.class);

    @ExceptionHandler(AuthenticationException.class)
    public Object handleAuthenticationException(AuthenticationException e) {
        logger.error("AuthenticationException", e);
        return Result.builder().isLogin(false).success(false).msg(e.getMessage()).code("401");
    }

    @ExceptionHandler(PermissionException.class)
    public Object handlePermissionException(PermissionException e) {
        logger.error("AuthenticationException", e);
        return Result.builder().isLogin(false).success(false).msg(e.getMessage()).code("402");
    }

    @ExceptionHandler(BizException.class)
    public Object handleBizException(BizException e) {
        logger.error("AuthenticationException", e);
        return Result.builder().isLogin(true).success(false).msg(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        logger.error("Exception", e);
        return Result.builder().isLogin(true).success(false).msg("服务器开小差啦");
    }

}
